package com.song.controller.file;

import com.song.common.Result;
import com.song.entity.Constants;
import com.song.service.file.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("${api.version}/file")
public class FileController {

    @Autowired
    private FileService fileService;

    @RequestMapping("/fileManage")
    public String fileManage(){
        return "file/fileManage";
    }

    /**
     * 文件上传
     */
    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    @ResponseBody
    public Result upload(@RequestParam("file") MultipartFile file) {
        Result result = new Result();
        try {
            result = fileService.upload(file);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(Constants.FAIL_CODE);
            result.setMsg("出现异常");
        }
        return result;
    }

    /**
     * 多文件上传
     */
     @RequestMapping(value = "/uploadFiles",method = RequestMethod.POST)
     @ResponseBody
     public Result uploadFiles(MultipartFile[] file){
         Result result = new Result();
         try {
             result = fileService.uploadFiles(file);
         } catch (Exception e) {
             e.printStackTrace();
             result.setCode(Constants.FAIL_CODE);
             result.setMsg("出现异常");
         }
         return result;
     }
}
